﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Demo
{
    public partial class SimpleBinding : Form
    {
        public class Member : INotifyPropertyChanged
        {
            private string userName = "";
            private string password = "";

            public string UserName
            {
                get { return userName; }
                set
                {
                    userName = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged.Invoke(this, new PropertyChangedEventArgs("UserName"));
                    }
                }
            }

            public string Password
            {
                get { return password; }
                set
                {
                    password = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Password"));
                    }
                }
            }

            public Member(string userName, string passWord)
            {
                this.UserName = userName;
                this.Password = passWord;
            }

            public event PropertyChangedEventHandler PropertyChanged;


        }

        Member member = new Member("Mr A", "123");

        public SimpleBinding()
        {
            InitializeComponent();

            Binding userNameBinding = new Binding("Text", member, "UserName");
            Binding passwordBinding = new Binding("Text", member, "Password");

            userNameText.DataBindings.Add(userNameBinding);
            passwordText.DataBindings.Add(passwordBinding);
        }

        private void showInfoButton_Click(object sender, EventArgs e)
        {
            infoLabel.Text = string.Format("User name : {0}, Password : {1}", member.UserName, member.Password);
        }

        private void changeButton_Click(object sender, EventArgs e)
        {
            member.UserName += DateTime.Now.Second;
            member.Password += DateTime.Now.Second;
        }
    }
}
